﻿using System;
using System.ComponentModel;
using EventAggregator;

namespace EAWpfSample
{
    public sealed class MainWindowViewModel : INotifyPropertyChanged
    {
        private DateTime _dateTime;
        private DoWorkCommand _doWorkCommand;

        public MainWindowViewModel()
        {
            this.Subscribe(EventId.Default.TimerUpdate, HandleTimerUpdate);
        }

        public DoWorkCommand DoWorkCommand
        {
            get { return _doWorkCommand ?? (_doWorkCommand = new DoWorkCommand()); }
        }

        public DateTime DateTime
        {
            get { return _dateTime; }

            set
            {
                if (_dateTime == value)
                {
                    return;
                }

                _dateTime = value;

                RaisePropertyChanged("DateTime");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void HandleTimerUpdate(EventMessage eventMessage)
        {
            DateTime = eventMessage.GetValue<DateTime>("DateTime");
        }

        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}